package com.hrrt.home.admin.controller.manage;

import com.hrrt.common.base.BaseController;
import com.hrrt.home.common.constant.HomeResult;
import com.hrrt.home.common.constant.HomeResultConstant;
import com.hrrt.home.dao.model.HomeDeviceType;
import com.hrrt.home.dao.model.HomeDeviceTypeExample;
import com.hrrt.home.rpc.api.HomeDeviceTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 设备类型 controller
 * Created by JiangyanPeng on 2017/09/01.
 */

@Controller
@RequestMapping("/manage/devicetype")
@Api(value = "设备类型", description = "设备类型")
public class HomeDeviceTypeController extends BaseController {
    @Autowired
    HomeDeviceTypeService homeDeviceTypeService;
    private static Logger _log = LoggerFactory.getLogger(HomeBaseUnitController.class);
    @ApiOperation(value = "设备类型首页")
    @RequiresPermissions("home:devicetype:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/devicetype/index.jsp";
    }
    @ApiOperation(value = "设备类型列表")
    @RequestMapping(value="list",method = RequestMethod.GET)
    @RequiresPermissions("home:devicetype:read")
    @ResponseBody
    public Object list (@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
                        @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
                        @RequestParam(required = false, value = "sort") String sort,
                        @RequestParam(required = false, value = "order") String order){
        HomeDeviceTypeExample homeDeviceTypeExample = new HomeDeviceTypeExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            homeDeviceTypeExample.setOrderByClause(sort + " " + order);
        }
        List<HomeDeviceType> rows = homeDeviceTypeService.selectByExampleForOffsetPage(homeDeviceTypeExample,offset,limit);
        long total = homeDeviceTypeService.countByExample(homeDeviceTypeExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }
    @ApiOperation(value = "新增设备类型")
    @RequiresPermissions("home:devicetype:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create() {
        return "/manage/devicetype/create.jsp";
    }

    @ApiOperation(value = "新增设备类型")
    @RequiresPermissions("home:devicetype:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(HomeDeviceType homeDeviceType) {
        int count = homeDeviceTypeService.insertSelective(homeDeviceType);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

    @ApiOperation(value = "修改设备类型")
    @RequiresPermissions("home:devicetype:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        HomeDeviceType homeDeviceType = homeDeviceTypeService.selectByPrimaryKey(id);
        modelMap.put("homeDeviceType", homeDeviceType);
        return "/manage/devicetype/update.jsp";
    }

    @ApiOperation(value = "修改设备类型")
    @RequiresPermissions("home:devicetype:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") int id, HomeDeviceType homeDeviceType) {
        homeDeviceType.setId(id);
        int count = homeDeviceTypeService.updateByPrimaryKeySelective(homeDeviceType);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

    @ApiOperation(value = "删除基础设备")
    @RequiresPermissions("home:devicetype:delete")
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = homeDeviceTypeService.deleteByPrimaryKeys(ids);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }
}
